﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UMC.Data;
using UMC.Data.Sql;
using UMC.Web;
using UMC.WorkFlow.Entities;

namespace UMC.WorkFlow
{
    class WorkFlowDbProviderActivity : UMC.Web.Activities.SystemSetupActivity
    {


        public override void ProcessActivity(WebRequest request, WebResponse response)
        {

            var key = this.AsyncDialog("Key", g => this.DialogValue("Promotion"));
            if(key== "Master" && request.IsMaster)
            {

                var ProviderName = this.AsyncDialog("ProviderName", r => new UITextDialog() { Title = "数据库配置名称" });
                this.DataBase(ProviderName);
                this.Context.Send($"{request.Model}.{request.Command}", true);

            }
            else
            {
                this.Context.UIEvent(key, new ListItem(this.AsyncDialog("database", g =>
                {
                    var proms = UMC.Data.Reflection.Configuration("database").Providers.GetEnumerator();
                    var select = new List<WebMeta>();
                    while (proms.MoveNext())
                    {
                        select.Add(new WebMeta().Put("name", proms.Current.Name,"type",proms.Current.Type));

                    }

                    var gl = UIDialog.Create(new UIGridDialog.Header("name", 0).PutField("name", "节点").PutField("type", "类型"), select.ToArray());
                    gl.Title = "数据库节点";
                    if (request.IsCashier)
                    {
                        gl.Menu(new UIClick(request, "Key", "Master") { Text = "新增" });
                    }
                    gl.CloseEvent = "UI.Event";
                    gl.RefreshEvent = $"{request.Model}.{request.Command}";
                    return gl;
                })), true);
            }



        }

    }
}
